package net.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.pojo.*;
import net.service.CategoryService;
import net.service.NoticeService;
import net.service.OrderService;
import net.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

@Controller
@RequestMapping("/main")
public class CategoryController {
    final String SALE_DAY_SEVEN = "7";
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    OrderService orderService;

    @RequestMapping("/index")
    public String searchProduct(Model model) {
        Notice noticeList = noticeService.showById();
        List<Product> productList =productService.countHotSaleProducts(SALE_DAY_SEVEN);
        model.addAttribute("n", noticeList);
        model.addAttribute("pList",productList);
        return "/client/index.jsp";
    }

    //    @RequestMapping("/showPageByCid")
//    public String showPageByCid(int cid,Model model){
//        List<Category> list = categoryService.findAll();
//        List<ProductJo> productJoList = productService.findByCid(cid);
//        Category category = categoryService.findByCid(cid);
//        int count = productJoList.size();
//        model.addAttribute("count",count);
//        model.addAttribute("c",category);
//        model.addAttribute("list",list);
//        model.addAttribute("clist",productJoList);
//        return "/client/product_list.jsp";
//    }
//    @RequestMapping("/findAllProduct")
//    public String findAllProduct(Model model){
//        List<Category> list = categoryService.findAll();
//        List<Product> clist = productService.findAll();
//        int count = clist.size();
//        Category category = new Category();
//        category.setName("全部商品类别");
//        model.addAttribute("c",category);
//        model.addAttribute("count",count);
//        model.addAttribute("list",list);
//        model.addAttribute("clist",clist);
//        return "/client/product_list.jsp";
//    }
    @RequestMapping("/searchProduct")
    public String searchProduct(String name, Model model) {
        List<Product> clist = productService.searchProduct(name);
        int count = clist.size();
        model.addAttribute("count", count);
        model.addAttribute("clist", clist);
        return "/client/product_search_list.jsp";
    }

    @RequestMapping("/info")
    public String findById(String id, Model model) {
        Product product = productService.findById(id);
        model.addAttribute("p", product);
        return "/client/info.jsp";
    }

    @RequestMapping("/pageProduct")
    public String pageProduct(Integer currentPage,
                              @RequestParam(required = false) Integer cid,
                              Model model) {
        PageHelper.startPage(currentPage, 4);
        if (cid == null){
            List<Product> productJoList = productService.findAll();
            PageInfo info = new PageInfo(productJoList);
            Category category = new Category();
            category.setName("全部商品类别");
            model.addAttribute("c", category);
            model.addAttribute("info", info);
            return "/client/product_Pagelist.jsp";
        }else {
            List<ProductJo> productJoList = productService.findByCid(cid);
            PageInfo info = new PageInfo(productJoList);
            Category category = categoryService.findByCid(cid);
            model.addAttribute("c", category);
            model.addAttribute("info", info);
            return "/client/product_Pagelist.jsp";
        }
    }
    @RequestMapping("/category")
    @ResponseBody
    public List<Category> getCagegory(){
        List<Category> list = categoryService.findAll();
        return list;
    }
}